<template>
	<view class="wrap">
		<u-navbar :title="$t('bdsjh')" title-width="340" :border-bottom="false" back-icon-color="#fff" title-color="#fff" :background="{ backgroundColor: '#0030A8' }"></u-navbar>
		<view class="tom-container">
			<view class="list">
				<view class="row">
					<view class="row-label">{{$t('sjhm')}}</view>
					<view class="row-main">
						<input type="number" v-model="phone" maxlength="11" :placeholder="$t('qsrsjh')" placeholder-class="place-class"/>
					</view>
				</view>
				<view class="row">
					<view class="row-label">{{$t('yzm')}}</view>
					<view class="row-main">
						<input type="text" v-model="verificationCode" maxlength="6" :placeholder="$t('qsryzm')" placeholder-class="place-class"/>
						<text @tap="getCode" class="row-main-tip">{{tips}}</text>
					</view>
				</view>
				<view class="login_btn" @tap="submit">{{$t('qrbd')}}</view>
			</view>
		</view>
		<u-verification-code :seconds="seconds" ref="uCode" @change="codeChange"></u-verification-code>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone:'',// 手机号码
				verificationCode:'',// 验证码
				tips:this.$t('hqyzm'),//倒计时提示文字
				seconds:60,
			}
		},
		methods: {
			// 确定
			submit(){
				let that = this
				let reg=new RegExp('^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$');
				if(!this.$u.test.mobile(this.phone)) return this.$utils.toast('请输入正确的手机号');
				if(this.$u.test.isEmpty(this.verificationCode)) return this.$utils.toast('请输入验证码');
				this.$utils.loading(this.$t('qsh'))
				this.$api.loginApi.bindPhoneOrMailAccount({
					phone:this.phone,
					verificationCode:this.verificationCode,
				})
				.then(res=>{
					this.$utils.toast(res.data)
					uni.switchTab({
					    url: '/pages/User/User'
					});
				})
			},
			// 倒计时变化
			codeChange(text) {
				this.tips = text;
			},
			// 获取验证码
			getCode() {
				if(!this.$u.test.mobile(this.phone)) return this.$utils.toast(this.$t('qsrzqsjh'));
				if(this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: this.$t('zzhq')
					})
					this.$api.loginApi.getMsg({
						phone:this.phone
					})
					.then(res=>{
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$utils.toast(this.$t('yzmyfs'));
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
.tom-container {
	padding-top: 50rpx;
}
.list {
	flex: 1;
	display: flex;
	flex-direction: column;
	border-radius: 50rpx 50rpx 0rpx 0rpx;
	overflow: hidden;
	background-color: #fff;
	background-image: url('/static/new_images/showPopup.png');
	background-position: left bottom;
	background-repeat: no-repeat;
	background-size: 100% 800rpx;
	.row {
		margin: 30rpx;
	
		&:first-child {
			margin-top: 40rpx;
		}
	
		&-label {
			color: $tom-text-color;
			font-size: 28rpx;
			font-weight: bold;
		}
	
		&-main {
			margin-top: 28rpx;
			background-color: #FAFAFA;
			height: 68rpx;
			padding: 0 20rpx;
			display: flex;
			align-items: center;
	
			.place-class {
				color: #CCCCCC;
			}
	
			input {
				flex: 1;
				color: $tom-text-color;
				font-size: 24rpx;
			}
	
			&-tip {
				margin-left: 20rpx;
				color: $tom-text-color;
				font-size: 24rpx;
			}
		}
	}
}
</style>
